﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using log4net;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.ShopManage.Def;
using CashFlow.ShopManage.Common;

namespace CashFlow.ShopManage.Service.Implement
{
    public class AdminService : Service<AdminEntity>, IAdminService
    {
        private static ILog _logger = LogManager.GetLogger(typeof(AdminService));

        public bool Login(string username, string pwd)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<AdminEntity>()
                                .Where(x => x.UserName == username
                                    && x.Pwd == pwd
                                    && x.Type == 1)
                                .First();
                if (entity != null)
                {
                    HttpContext.Current.Session[Consts.SESSIONKEY_LOGINUSERINFO] = entity;

                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public AdminEntity GetLoginUserInfo()
        {
            var entity = HttpContext.Current.Session[Consts.SESSIONKEY_LOGINUSERINFO] as AdminEntity;
            return entity;
        }

        public bool IsLogin()
        {
            var entity = GetLoginUserInfo();
            return entity != null;
        }

        public IList<AdminShopInfoView> QueryAll2()
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<AdminEntity>()
                                .LeftJoin<ShopInfoEntity>((a, s) => a.ShopManagerUserId == s.ShopManagerUserId)
                                .Select((a, s) => new AdminShopInfoView
                                {
                                    AdminInfo = a,
                                    ShopInfo = s
                                })
                                .ToList();
                return ls;
            }
        }
    }
}